package com.gitee.target123.develop.common.httpclient;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import javax.net.ssl.SSLContext;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;

/**
 * httpClient连接池
 * 获取唯一请求客户端对象 CloseableHttpClient
 */
public class HttpClientPool {
    //连接池最大线程数
    private final int httpMaxPoolSize;
    //获取/建立连接超时
    private final int connectTimeout;
    //获取数据超时
    private final int socketTimeout;
    //最大空间时间
    private final int maxIdleTime;

    //发送请求的客户端单例
    private CloseableHttpClient httpClient;

    public HttpClientPool(int httpMaxPoolSize,int connectTimeout,
                          int socketTimeout,int maxIdleTime) {
        this.httpMaxPoolSize = httpMaxPoolSize;
        this.connectTimeout = connectTimeout;
        this.socketTimeout = socketTimeout;
        this.maxIdleTime = maxIdleTime;
        init();
    }

    private void init() {
        HttpClientBuilder httpClientBuilder = HttpClients.custom();
        RequestConfig requestConfig = RequestConfig.custom()
                // 获取连接超时时间
                .setConnectionRequestTimeout(connectTimeout)
                // 连接建立超时时间
                .setConnectTimeout(connectTimeout)
                // 获取数据超时时间
                .setSocketTimeout(socketTimeout)
                .build();
        httpClientBuilder.setDefaultRequestConfig(requestConfig);
        try {
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
                    .register("https", new SSLConnectionSocketFactory(SSLContext.getDefault()))
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .build();
            //连接池管理器
            PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            // 最大连接数
            connectionManager.setMaxTotal(httpMaxPoolSize);
            // 路由最大连接数
            connectionManager.setDefaultMaxPerRoute(httpMaxPoolSize);

            httpClientBuilder.setConnectionManager(connectionManager)
                    //清理超过空闲时间的连接
                    .evictIdleConnections(maxIdleTime, TimeUnit.MILLISECONDS);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        this.httpClient = httpClientBuilder.build();
    }

    public CloseableHttpClient getHttpClient() {
        return this.httpClient;
    }
}
